import { isFunction } from "./util/isFunction";
// Subscriber unsubscribe 取消订阅方法 ，继承于Subscription
// Subscriber add 收集取消方法的方法 ，继承于Subscription

import { Subscription } from "./Subscription";
export class Subscriber extends Subscription {
  // 执行 complete 后，再执行 next, next 无效
  isStopped = false;
  constructor(observerOrNext) {
    super();
    let observer;
    // 兼容 const observer2 = next: (value) => {console.log("next value", value);} 的写法，包装成对象.........
    if (isFunction(observerOrNext)) {
      observer = {
        next: observerOrNext,
      };
    } else {
      observer = observerOrNext;
    }
    // ...............................................................................
    //把观察者对象存到了订阅者对象的destination属性上
    this.destination = observer;
  }
  next(value) {
    if (!this.isStopped) {
      this.destination.next(value);
    }
  }
  //如果调用了complete方法，就表示生产完毕了
  complete() {
    if (!this.isStopped) {
      this.isStopped = true;
      this.destination.complete?.();
    }
  }
}
